home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtoatk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  4.5 KB  |  192 lines

  1. /* pbmtoatk.c - convert portable bitmap to Andrew Toolkit raster object
  2. **
  3. ** Copyright (C) 1991 by Bill Janssen.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "pbm.h"
  15.  
  16. #define DEFAULTSCALE (1<<16)
  17. #define RASTERVERSION 2
  18. #define TRUE        1
  19. #define FALSE        0
  20.  
  21. void
  22. main( argc, argv )
  23.     int argc;
  24.     char *argv[];
  25.     {
  26.     FILE *ifd;
  27.     bit *bitrow;
  28.     register bit *bP;
  29.     int rows, cols, format, padright, row;
  30.     register int col;
  31.     char name[100], *cp;
  32.     static char hexchar[] = "0123456789abcdef";
  33.     unsigned char curbyte, newbyte;
  34.     int curcount, gather, line;
  35.  
  36.     pbm_init ( &argc, argv );
  37.  
  38.     if ( argc > 2 )
  39.     pm_usage( "[pbmfile]" );
  40.  
  41.     if ( argc == 2 )
  42.     {
  43.     ifd = pm_openr( argv[1] );
  44.     strcpy( name, argv[1] );
  45.     if ( strcmp( name, "-" ) == 0 )
  46.         strcpy( name, "noname" );
  47.  
  48.     if ( ( cp = index( name, '.' ) ) != 0 )
  49.         *cp = '\0';
  50.     }
  51.     else
  52.     {
  53.     ifd = stdin;
  54.     strcpy( name, "noname" );
  55.     }
  56.  
  57.     pbm_readpbminit( ifd, &cols, &rows, &format );
  58.     bitrow = pbm_allocrow( cols );
  59.  
  60.     /* Compute padding to round cols up to the nearest multiple of 16. */
  61.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  62.  
  63.     fprintf (stdout, "\\begindata{raster,%d}\n", 1);
  64.     fprintf (stdout, "%ld %ld %ld %ld ", RASTERVERSION, 
  65.          0, DEFAULTSCALE, DEFAULTSCALE);
  66.     fprintf (stdout, "%ld %ld %ld %ld\n",
  67.          0, 0, cols, rows);    /* subraster */
  68.     fprintf (stdout, "bits %ld %ld %ld\n", 1, cols, rows);
  69.  
  70.     for ( row = 0; row < rows; row++ )
  71.     {
  72.       pbm_readpbmrow( ifd, bitrow, cols, format );
  73.       bP = bitrow;
  74.       gather = 0;
  75.       newbyte = 0;
  76.       curbyte = 0;
  77.       curcount = 0;
  78.       col = 0;
  79.       while (col < cols)
  80.         {
  81.           if (gather > 7)
  82.         {
  83.           process_atk_byte (&curcount, &curbyte, stdout, newbyte, FALSE);
  84.           gather = 0;
  85.           newbyte = 0;
  86.         }
  87.           newbyte = (newbyte << 1) | (*bP++);
  88.           gather += 1;
  89.           col += 1;
  90.         }
  91.  
  92.       if (gather > 0)
  93.         {
  94.           newbyte = (newbyte << (8 - gather));
  95.           process_atk_byte (&curcount, &curbyte, stdout, newbyte, TRUE);
  96.         }
  97.         }
  98.  
  99.     pm_close( ifd );
  100.     
  101.     fprintf (stdout, "\\enddata{raster, %d}\n", 1);
  102.  
  103.     pm_close (stdout);
  104.     exit( 0 );
  105.   }
  106.  
  107. write_atk_bytes (file, curbyte, curcount)
  108.      FILE *file;
  109.      unsigned char curbyte;
  110.      unsigned int curcount;
  111. {
  112.     /* codes for data stream */
  113. #define WHITEZERO    'f'
  114. #define WHITETWENTY    'z'
  115. #define BLACKZERO    'F'
  116. #define BLACKTWENTY    'Z'
  117. #define OTHERZERO    0x1F
  118.  
  119. #define    WHITEBYTE    0x00
  120. #define    BLACKBYTE    0xFF
  121.  
  122.     /* WriteRow table for conversion of a byte value to two character
  123. hex representation */
  124.  
  125.     static unsigned char hex[16] = {
  126.     '0', '1', '2', '3', '4', '5', '6', '7',
  127.     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  128.     };
  129.  
  130.   switch (curbyte) {
  131.   case WHITEBYTE:
  132.     while (curcount > 20) 
  133.       fputc(WHITETWENTY, file),
  134.       curcount -= 20;
  135.     fputc(WHITEZERO + curcount, file);
  136.     break;
  137.   case BLACKBYTE:
  138.     while (curcount > 20) 
  139.       fputc(BLACKTWENTY, file),
  140.       curcount -= 20;
  141.     fputc(BLACKZERO + curcount, file);
  142.     break;
  143.   default:
  144.     while (curcount > 16)
  145.       fputc(OTHERZERO+16, file),
  146.       fputc(hex[curbyte / 16], file),
  147.       fputc(hex[curbyte & 15], file),
  148.       curcount -= 16;
  149.     if (curcount > 1)
  150.       fputc(OTHERZERO+curcount, file);
  151.     else ;  /* the byte written will represent a single instance */
  152.     fputc(hex[curbyte / 16], file);
  153.     fputc(hex[curbyte & 15], file);
  154.   }
  155. }
  156.  
  157. process_atk_byte (pcurcount, pcurbyte, file, newbyte, eolflag)
  158. int *pcurcount;
  159. unsigned char *pcurbyte;
  160. FILE *file;
  161. unsigned char newbyte;
  162. int eolflag;
  163. {
  164.     int curcount = *pcurcount;
  165.     unsigned char curbyte = *pcurbyte;
  166.  
  167.     if (curcount < 1)
  168.     {
  169.     *pcurbyte = curbyte = newbyte;
  170.     *pcurcount = curcount = 1;
  171.     }
  172.     else if (newbyte == curbyte)
  173.     {
  174.     *pcurcount = (curcount += 1);
  175.     }
  176.  
  177.     if (curcount > 0 && newbyte != curbyte)
  178.       {
  179.     write_atk_bytes (file, curbyte, curcount);
  180.     *pcurcount = 1;
  181.     *pcurbyte = newbyte;
  182.       }
  183.  
  184.     if (eolflag)
  185.       {
  186.     write_atk_bytes (file, *pcurbyte, *pcurcount);
  187.     fprintf (file, " |\n");
  188.     *pcurcount = 0;
  189.     *pcurbyte = 0;
  190.       }
  191. }
  192.